package com.migu.rbac.service;

import com.migu.rbac.model.SysRole;
import com.migu.rbac.repository.SysRoleMenuRepository;
import com.migu.rbac.vo.PageInfoRespone;
import com.migu.rbac.vo.page.RolePage;
import com.migu.rbac.repository.SysRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description TODO :
 * @Author: zj
 * @Date: 9:59 2019/4/4
 * @Version 1.0
 **/
@Service
@Transactional
public class SysRoleService {

    @Autowired
    SysRoleRepository roleRepository;
    @Autowired
    SysRoleMenuRepository roleMenuRepository;

    //添加角色
    public Boolean saveRole(SysRole sysRole) {
        if (roleRepository.selectRoleIsExists(sysRole) > 0) {
            return false;
        }
        if (roleRepository.saveRole(sysRole) > 0) {
            return true;
        } else {
            return false;
        }
    }

    //删除角色
    public Boolean deleteRole(int roleid) {
        if (roleRepository.deleteRole(roleid) > 0) {
            //删除角色下所有菜单
            roleMenuRepository.deleteRoleMenu(roleid);
            return true;
        } else {
            return false;
        }
    }

    //修改角色
    public Boolean updateRole(SysRole sysRole) {
        if (roleRepository.selectRoleCountByUpdate(sysRole) > 0) {
            return false;
        }
        if (roleRepository.updateRole(sysRole) > 0) {
            return true;
        } else {
            return false;
        }
    }

    //查询所有角色组
    public List<SysRole> findAllRole() {
        return roleRepository.findAllRole();
    }


    //根据条件查询角色
    public PageInfoRespone selectRoleByExample(RolePage page) {
        PageInfoRespone respone = new PageInfoRespone();
        respone.setPageSize(page.getPageSize());
        respone.setPageNum(page.getPageNum());
        respone.setCount(roleRepository.selectRoleCount(page));

        page.setPageNum((page.getPageNum() - 1) * page.getPageSize());
        List<SysRole> sysRoles = roleRepository.selectRoleByExample(page);
        respone.setList(sysRoles);
        return respone;
    }


}
